Workflow Loop Template
The Workflow Loop template demonstrates how a BindAI workflow can repeatedly execute a workflow path while a predicate evaluates toTrue.
A LoopNode evaluates a predicate against the current workflow context. When the predicate is true, execution is directed to the loop body. When it is false, execution is directed to the loop exit.
This template introduces iterative workflow execution.
Purpose
This template demonstrates how to:- Add a
LoopNodeto a workflow - Evaluate a runtime predicate
- Execute a loop body repeatedly
- Maintain state through the workflow context
- Exit the loop when the predicate becomes false
- Build iterative workflow automation
Workflow Structure
ALoopNode requires two next nodes:
LoopNode interprets its next_nodes as:
LoopNode implementation.
LoopNode
A loop is created with a node ID and a predicate:Configuring the Loop
The loop must have at least two next nodes. The first node is the loop body:Execution Flow
When the executor reaches the loop, theLoopNode evaluates its predicate.
Loop Predicate
The predicate receives the workflow context. For example:Trueto execute the loop bodyFalseto execute the exit node
LoopNode executes.
Workflow Variables
Loop predicates commonly use workflow variables to track progress. For example:Basic Example
A simple loop can be structured conceptually as:True, the workflow proceeds to process-item.
When the predicate returns False, the workflow proceeds to loop-complete.
Iterative Processing
A loop is useful when a workflow needs to repeatedly process data. For example:Batch Processing
Loops can be used for collection-oriented workflows. Conceptually:Polling
Loops can also represent repeated checks of runtime state. For example:Shared Workflow Context
Each workflow execution operates with a workflow context. The loop predicate receives that context directly:Loop Exit
The loop exits when the predicate returnsFalse.
next_nodes[1].
The exit node can then connect to additional workflow nodes.
For example:
EndNode required for a loop exit.
Loop vs Condition
A condition selects a path once when it executes. A loop can repeatedly evaluate its predicate.
Both use the workflow context to make runtime decisions.
Loop vs Retry
Loops and retries both involve repeated execution, but they serve different purposes.
Use a loop when repetition is part of the business process.
Use a retry policy when an operation fails and should be attempted again.
Preventing Infinite Loops
A loop should have a predicate that can eventually become false. For example:Error Handling
TheLoopNode itself raises an error if it does not have two next nodes:
Combining Loops with Conditions
Loops and conditions can be combined. For example:Combining Loops with Agents
An agent can participate in the loop body when repeated AI processing is required. For example:LoopNode itself does not perform AI operations.
Current Implementation Details
The current implementation has the following behavior:WorkflowExecutor subsequently uses context.current_node when determining what node to place into the execution queue.
Important Boundaries
Two next nodes are required
The current implementation requires at least two entries innext_nodes.
They represent:
The predicate receives the context
The loop predicate receives the complete workflow context:The loop does not execute its body directly
The loop setscontext.current_node.
The workflow executor is responsible for continuing execution.
No built-in delay
TheLoopNode does not provide a built-in waiting period between iterations.
Polling workflows that require delays need another mechanism for waiting.
No automatic iteration counter
TheLoopNode does not maintain a built-in iteration counter.
If a workflow needs one, store it in the workflow context or another appropriate state mechanism.
Best Practices
- Always configure both the loop body and exit node.
- Make the predicate deterministic and easy to understand.
- Update the state used by the predicate during the loop body.
- Ensure the loop has a realistic termination condition.
- Keep loop bodies focused.
- Use retries for transient failures rather than business-process repetition.
- Test both loop continuation and loop exit.
- Test edge cases such as an empty collection or an already-satisfied condition.
- Avoid unnecessary nested loops.
- Add observability for workflows containing long or complex loops.
Related Templates
The loop pattern builds on the basic workflow model introduced in:08.1 workflow-basic.md
08.2 workflow-condition.md08.4 workflow-parallel.md08.5 workflow-human.md08.6 workflow-retry.md08.7 workflow-timeout.md08.8 workflow-schedule.md
Summary
The Workflow Loop template introduces iterative execution into BindAI workflows. The essential pattern is:LoopNode evaluates a predicate against the workflow context.
If the predicate returns True, the first configured next node is selected as the loop body.
If the predicate returns False, the second configured next node is selected as the loop exit.
The workflow executor then continues execution using the selected node.
This provides a simple mechanism for repeated business processing, collection handling, polling-style workflows, and other iterative workflow patterns.